package com.weng.bigdata.core.etl.enums;

import com.weng.bigdata.core.schedule.TaskStatusEnum;

/**
 * Streamsets运行状态
 * 编码说明(和TaskStatusEnum对应)：1x：未开始运行，2x:运行中，3x:执行成功,4x:执行失败,5x:强制停止
 *
 * @author wengzhonghui
 * @date: 2021-07-02 13:58:10
 */
public enum StreamsetsStatusEnum {

    EDITED(10, "EDITED", "管道作业已创建/修改，自创建/修改后未运行"),          // pipeline job has been create/modified, didn't run since the creation/modification

    STARTING(20, "STARTING", "管道作业初始化中..."),         // pipeline job starting (initialization)
    STARTING_ERROR(40, "STARTING_ERROR", "管道启动时失败（但销毁尚未完成）"),   // Pipeline failed while starting (but the destroy did not finished yet)
    START_ERROR(40, "START_ERROR", "管道作业在启动时失败（初始化期间）"),      // pipeline job failed while start (during initialization)

    RUNNING(20, "RUNNING", "管道作业运行中..."),          // pipeline job running
    RUNNING_ERROR(40, "RUNNING_ERROR", "管道作业在运行时失败（在管道上调用destroy）"),    // pipeline job failed while running (calling destroy on pipeline)
    RUN_ERROR(40, "RUN_ERROR", "管道运行失败"),        // pipeline job failed while running (done)

    FINISHING(20, "FINISHING", "管道结束中"),        // pipeline job finishing (source reached end, returning NULL offset) (calling destroy on pipeline)
    FINISHED(30, "FINISHED", "管道结束"),         // pipeline job finished                                              (done)
    RETRY(20, "RETRY", "重试中"),
    KILLED(50, "KILLED", "强制停止"),           // only happens in cluster mode


    STOPPING(20, "STOPPING", "正在停止"),         // pipeline job has been manually stopped (calling destroy on pipeline)
    STOPPED(30, "STOPPED", "已经停止"),          // pipeline job has been manually stopped (done)

    STOPPING_ERROR(40, "STOPPING_ERROR", "停止错误"),   // There was a problem when stopping pipeline
    STOP_ERROR(40, "STOP_ERROR", "停止错误"),       // Terminal state representing that pipeline failed to stop properly

    DISCONNECTING(70, "DISCONNECTING", "SDC优雅地下降（进行中）"),    // SDC going down gracefully (calling destroy on pipeline for LOCAL, doing nothing for CLUSTER)
    DISCONNECTED(70, "DISCONNECTED", "SDC优雅地下降（完成）"),     // SDC going down gracefully (done)

    CONNECTING(70, "CONNECTING", "SDC重新启动（对于仍在运行的群集检查作业，转换为本地启动）"),       // SDC starting back (transition to STARTING for LOCAL, for CLUSTER checks job still running)
    //                   (and transitions to RUNNING or RUN_ERROR -streaming- or FINISHED -batch)
    CONNECT_ERROR(70, "CONNECT_ERROR", "无法运行，在SDC上重新启动将重试"),    // failed to get to RUNNING, on SDC restart will retry again

    DELETED(40, "DELETED", "删除");

    private Integer code;

    private String name;

    private String remark;

    StreamsetsStatusEnum(Integer code, String name, String remark) {
        this.code = code;
        this.name = name;
        this.remark = remark;
    }

    public String getName() {
        return name;
    }

    public String getRemark() {
        return remark;
    }

    public Integer getCode() {
        return code;
    }

    /**
     * 根据Streamsets的状码，获取任务运行状态，见TaskStatusEnum
     *
     * @param status
     * @return
     */
    public static TaskStatusEnum getTaskStatusCode(Integer status) {
        if (status >= TaskStatusEnum.NOT_STARTED.getValue() && status < TaskStatusEnum.RUNNING.getValue()) {
            return TaskStatusEnum.NOT_STARTED;
        } else if (status >= TaskStatusEnum.RUNNING.getValue() && status < TaskStatusEnum.SUCCESS.getValue()) {
            return TaskStatusEnum.RUNNING;
        } else if (status >= TaskStatusEnum.SUCCESS.getValue() && status < TaskStatusEnum.FAILED.getValue()) {
            return TaskStatusEnum.SUCCESS;
        } else if (status >= TaskStatusEnum.FAILED.getValue() && status < TaskStatusEnum.STOPED.getValue()) {
            return TaskStatusEnum.FAILED;
        } else if (status >= TaskStatusEnum.STOPED.getValue() && status < TaskStatusEnum.STOPED.getValue()) {
            return TaskStatusEnum.STOPED;
        }
        return TaskStatusEnum.UN_KNOWN;
    }
}
